package com.sphd.emanu.activity.toptpcenter;

import android.content.Intent;
import android.os.Bundle;
import android.support.v4.widget.SwipeRefreshLayout;
import android.support.v7.widget.RecyclerView;
import android.support.v7.widget.Toolbar;
import android.widget.TextView;

import com.sphd.emanu.R;
import com.sphd.emanu.adapter.TopupcenterAdapter;
import com.sphd.emanu.config.Constant;
import com.sphd.emanu.mvp.MVPBaseActivity;
import com.sphd.emanu.pojo.login.User;
import com.sphd.emanu.pojo.vip.MemDeposit;
import com.sphd.emanu.pojo.vip.Member;
import com.sphd.emanu.pojo.vip.MemberForOrg;
import com.wzutils.BigDisMailUtils;

import java.util.ArrayList;

import butterknife.BindView;
import butterknife.ButterKnife;

/**
 * 充值中心
 *
 * @Author Hu
 * @Time 2017/6/28 -- 15:52.
 * @Email 87683202@qq.com
 */
public class ToptpcenterActivity extends MVPBaseActivity<ToptpcenterContract.View, ToptpcenterPresenter> implements ToptpcenterContract.View {

    @BindView(R.id.toolbar)
    Toolbar toolbar;
    @BindView(R.id.account_balance)
    TextView accountBalance;
    @BindView(R.id.acount_count_and_money)
    TextView acountCountAndMoney;
    @BindView(R.id.my_recycler)
    RecyclerView myRecycler;
    @BindView(R.id.my_swf)
    SwipeRefreshLayout mySwf;
    Intent intent;
    Bundle bundle;
    Member member;
    User user;
    TopupcenterAdapter topupcenterAdapter;
    MemberForOrg memberForOrg;
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_toptpcenter);
        ButterKnife.bind(this);
        initView();
        initEvent();
    }

    private void initEvent() {
        toolbar.setNavigationOnClickListener(v -> finish());
        mySwf.setOnRefreshListener(new SwipeRefreshLayout.OnRefreshListener() {
            @Override
            public void onRefresh() {
                addData();
            }
        });
    }

    private void initView() {
        setTitleBar(toolbar, "充值中心");
        intent = getIntent();
        bundle = intent.getExtras();
        user = (User) bundle.getSerializable(Constant.USER_ENTITY);
        member = (Member) bundle.getSerializable(Constant.MEMBER_ENTITY);
        memberForOrg= (MemberForOrg) bundle.getSerializable(Constant.MEMBER_FOR_ORG);
        accountBalance.setText("充值卡余额￥" + (memberForOrg.getMemBalance().getDeposit() == null ? "0" :
                memberForOrg.getMemBalance().getDeposit().toString()));
        initAdapter();
        addData();
    }

    private void initAdapter() {
        setRefresh(mySwf);
        setRecy(myRecycler);
        topupcenterAdapter=new TopupcenterAdapter(R.layout.item_top_up_center,new ArrayList<MemDeposit>());
        topupcenterAdapter.setEmptyView(emptyView);
        myRecycler.setAdapter(topupcenterAdapter);
    }

    private void addData() {
        showProgressDialog();
        mPresenter.findMemberDeposit(user.getOid(), member.getId());
    }

    @Override
    public void recBean(MemberForOrg memberForOrg) {
        dissProgressDialog();

        acountCountAndMoney.setText("共充值"+(memberForOrg.getOrderCount()==null?0:memberForOrg.getOrderCount())+
                "次，共充值"+(memberForOrg.getOrderSum()==null?0:memberForOrg.getOrderSum())+"元");
        topupcenterAdapter.setNewData(memberForOrg.getMemDeposit());
    }
}
